cc.Class({
    extends: cc.Component,

    ctor(){
        this.bgVideo = null;
        this.duration = 1;
        this.videoObj = null;
    },

    properties: {
        voicec: {
            default: null,
            type: cc.Node
        },
        voice: {
            default: null,
            type: cc.Node
        },
        bofang: {
            default: null,
            type: cc.Node
        },
        zanting: {
            default: null,
            type: cc.Node
        },
        chang: {
            default: null,
            type: cc.Node
        },
        jindu: {
            default: null,
            type: cc.Node
        },
        boBg: {
            default: null,
            type: cc.Node
        },
    },
    zantingF() {
        this.bgVideo.play()  
    },
    bofangF() {
        this.bgVideo.pause()       
    },
    voiceF() {
        this.bgVideo.mute = true    
        this.bgVideo.volume = 0.0;  

        this.videoObj.muted = true;
        this.videoObj.volume = 0;
    },
    voicecF() {
        this.bgVideo.mute = false  
        this.bgVideo.volume = 1.0;    

        this.videoObj.muted = false;
        this.videoObj.volume = 1;

    },
    boBgF(){

    },
    boBgE(event){
        this.videoObj.currentTime = this.bgVideo.currentTime = (event.getLocationX()-88)/(658-88)*this.duration;
    },
    onLoad () {
        this.bgVideo = cc.find('Canvas/video').getComponent(cc.VideoPlayer);
        this.videoObj = this.bgVideo._impl._video;
        this.zanting.on(cc.Node.EventType.TOUCH_START, this.zantingF, this);
        this.bofang.on(cc.Node.EventType.TOUCH_START, this.bofangF, this);
        this.voice.on(cc.Node.EventType.TOUCH_START, this.voiceF, this);
        this.voicec.on(cc.Node.EventType.TOUCH_START, this.voicecF, this);
        this.boBg.on(cc.Node.EventType.TOUCH_START, this.boBgF, this);
        this.boBg.on(cc.Node.EventType.TOUCH_END, this.boBgE, this);
        this.boBg.on(cc.Node.EventType.TOUCH_CANCEL, this.boBgE, this);
        this.chang.width = 0;
        this.jindu.x = -284;
        this.bofang.active = false;
        this.voicec.active = false;
        this.duration = this.bgVideo.getDuration() || this.videoObj.duration;
        console.log(this.videoObj)
    },
    onDestroy () {
        console.log('ctor Destory')
        this.zanting.off(cc.Node.EventType.TOUCH_START, this.zantingF, this);
        this.bofang.off(cc.Node.EventType.TOUCH_START, this.bofangF, this);
        this.voice.off(cc.Node.EventType.TOUCH_START, this.voiceF, this);
        this.voicec.off(cc.Node.EventType.TOUCH_START, this.voicecF, this);
        this.boBg.off(cc.Node.EventType.TOUCH_START, this.boBgF, this);
        this.boBg.off(cc.Node.EventType.TOUCH_END, this.boBgE, this);
        this.boBg.off(cc.Node.EventType.TOUCH_CANCEL, this.boBgE, this);
    },
    start () {

    },
    //总长284
    update (dt) {
        let curTime = this.bgVideo.currentTime || this.videoObj.currentTime;
        let w = 568*curTime/this.duration;
        this.chang.width = w;
        this.jindu.x = (-284 + w)

        if(curTime/this.duration>=1){
            console.log('视频播放完')
            this.bgVideo.currentTime = 0;
            this.bgVideo.play()
        }
        
        if(this.videoObj.muted){
            this.voice.active = false
            this.voicec.active = true    
        }else{
            this.voice.active = true
            this.voicec.active = false    
        }

        if(!this.bgVideo.isPlaying()){
            this.zanting.active = true
            this.bofang.active = false   
        }else{
            this.zanting.active = false
            this.bofang.active = true      
        }
    },
});
